#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <sqlcli1.h>

#include <string>
#include <vector>
using namespace std;

#include "simbat_plugin.h"

int SQL_NOTFND  = 100;
int SQL_DUPKEY1 = -803;
int SQL_DUPKEY2 = -803;
int SQL_DUPREC  = -811;
int SQL_TABNEX  = -204;

struct H_SIMBAT_STATUS
{
   char COL_SYSTEM[41];
   long COL_IDX;
   char COL_REC_TYPE[11];
   char COL_VAL[2001];
} SIMBAT_STATUS;

struct H_SIMBAT_HIS
{
   char COL_SYSTEM[41];
   char COL_BATCH_SEQ[21];
   char COL_JOB_NAME[81];
   char COL_START_TIME[21];
   char COL_END_TIME[21];
   long COL_ELAPSE;
   char COL_STATUS[11];
} SIMBAT_HIS;

struct H_SIMBAT_STATISTIC
{
   char COL_SYSTEM[41];
   char COL_BATCH_TYPE[11];
   char COL_JOB_NAME[81];
   long COL_N;
   long COL_ELAPSE0;
   long COL_ELAPSE1;
   long COL_ELAPSE2;
   long COL_ELAPSE3;
   long COL_ELAPSE4;
   long COL_ELAPSE5;
   long COL_ELAPSE6;
   long COL_ELAPSE7;
   long COL_ELAPSE8;
   long COL_ELAPSE9;
} SIMBAT_STATISTIC;

static SQLHANDLE EnvHandle             = SQL_NULL_HANDLE;
static SQLHANDLE ConnectHandle         = SQL_NULL_HANDLE;
static SQLHANDLE CountStmtHandle       = SQL_NULL_HANDLE;
static SQLHANDLE MaxStmtHandle         = SQL_NULL_HANDLE;
static SQLHANDLE DeleteStmtHandle      = SQL_NULL_HANDLE;
static SQLHANDLE CommitStmtHandle      = SQL_NULL_HANDLE;
static SQLHANDLE InsertStmtHandle      = SQL_NULL_HANDLE;
static SQLHANDLE InsertHisStmtHandle   = SQL_NULL_HANDLE;
static SQLHANDLE InsertStaStmtHandle   = SQL_NULL_HANDLE;
static SQLHANDLE SelectStmtHandle      = SQL_NULL_HANDLE;
static SQLHANDLE SelectStaStmtHandle   = SQL_NULL_HANDLE;
static SQLHANDLE UpdateStmtHandle      = SQL_NULL_HANDLE;
static SQLHANDLE UpdateHisStmtHandle   = SQL_NULL_HANDLE;
static SQLHANDLE UpdateStaStmtHandle   = SQL_NULL_HANDLE;

static void PrintError(const char *Msg, SQLHANDLE Handle, SQLSMALLINT Type, char ErrStr[200 + 1])
{
   int Len = 0;
   char Sqlstate[5 + 1];
   SQLSMALLINT BufferLen;

   if ( Handle == SQL_NULL_HANDLE )
   {
      return;
   }

   snprintf(ErrStr, 200, "%s, ", Msg);
   Len = strlen(ErrStr);
   SQLGetDiagRec(Type, Handle, 1, (SQLCHAR*)Sqlstate, 0, (SQLCHAR*)ErrStr + Len, 200 - Len, &BufferLen);
   ErrStr[Len + BufferLen] = '\0';
}

static int DbConnect(const char *System, char ErrStr[200 + 1])
{
   void *Handle = NULL;
   int r;
   char DbServerName[81];
   char User[81];
   char Pass[81];
   char Stmt[501];

   ErrStr[0] = '\0';
   Handle = SimbatOpenConfig(ErrStr);

   if ( Handle == NULL )
   {
      return 8;
   }

   SimbatConfigGetValue(Handle, "DATABASE", "SERVER", DbServerName, 80);
   SimbatConfigGetValue(Handle, "DATABASE", "USER", User, 80);
   SimbatConfigGetPassword(Handle, "DATABASE", "PASSWORD", Pass, 80);
   r = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &EnvHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLAllocHandle(ENV) fail", SQL_NULL_HANDLE, 0, ErrStr);
      return 8;
   }

   r = SQLSetEnvAttr(EnvHandle, SQL_ATTR_ODBC_VERSION,
      (SQLPOINTER)SQL_OV_ODBC3, 0);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLSetEnvAttr fail", EnvHandle, SQL_HANDLE_ENV, ErrStr);
      return 8;
   }

   r = SQLAllocHandle(SQL_HANDLE_DBC, EnvHandle, &ConnectHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLAllocHandle(DBC) fail", EnvHandle, SQL_HANDLE_ENV, ErrStr);
      return 8;
   }

   r = SQLSetConnectAttr(ConnectHandle, SQL_LOGIN_TIMEOUT,
      (SQLPOINTER)5, 0);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLSetConnectAttr fail" , ConnectHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLConnect(ConnectHandle, (SQLCHAR*)DbServerName, SQL_NTS,
      (SQLCHAR*)User, SQL_NTS, (SQLCHAR*)Pass, SQL_NTS);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLConnect fail", ConnectHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLAllocHandle(SQL_HANDLE_STMT, ConnectHandle, &CountStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLAllocHandle(STMT) fail" , ConnectHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   snprintf(Stmt, 500, "SELECT COUNT(*) FROM SIMBAT_STATUS WHERE COL_SYSTEM = ?");

   r = SQLPrepare(CountStmtHandle, (SQLCHAR*)Stmt, SQL_NTS);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLPrepare(count) fail" , CountStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(CountStmtHandle, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 40, 0,
      SIMBAT_STATUS.COL_SYSTEM, sizeof(SIMBAT_STATUS.COL_SYSTEM), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(count, 1) fail" , CountStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLAllocHandle(SQL_HANDLE_STMT, ConnectHandle, &MaxStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLAllocHandle(STMT) fail" , ConnectHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   snprintf(Stmt, 500, "SELECT VALUE(MAX(COL_IDX), 0) FROM SIMBAT_STATUS WHERE COL_SYSTEM = ?");

   r = SQLPrepare(MaxStmtHandle, (SQLCHAR*)Stmt, SQL_NTS);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLPrepare(max) fail" , MaxStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(MaxStmtHandle, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 40, 0,
      SIMBAT_STATUS.COL_SYSTEM, sizeof(SIMBAT_STATUS.COL_SYSTEM), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(max, 1) fail" , MaxStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLAllocHandle(SQL_HANDLE_STMT, ConnectHandle, &DeleteStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLAllocHandle(STMT) fail" , ConnectHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   snprintf(Stmt, 500, "DELETE FROM SIMBAT_STATUS WHERE COL_SYSTEM = ? AND COL_IDX < ?");

   r = SQLPrepare(DeleteStmtHandle, (SQLCHAR*)Stmt, SQL_NTS);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLPrepare(delete) fail" , DeleteStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(DeleteStmtHandle, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 40, 0,
      SIMBAT_STATUS.COL_SYSTEM, sizeof(SIMBAT_STATUS.COL_SYSTEM), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(delete, 1) fail" , DeleteStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(DeleteStmtHandle, 2, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATUS.COL_IDX, sizeof(SIMBAT_STATUS.COL_IDX), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(delete, 2) fail" , DeleteStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLAllocHandle(SQL_HANDLE_STMT, ConnectHandle, &CommitStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLAllocHandle(STMT) fail" , ConnectHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   snprintf(Stmt, 500, "COMMIT WORK");

   r = SQLPrepare(CommitStmtHandle, (SQLCHAR*)Stmt, SQL_NTS);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLPrepare(commit) fail" , CommitStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLAllocHandle(SQL_HANDLE_STMT, ConnectHandle, &InsertStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLAllocHandle(STMT) fail" , ConnectHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   snprintf(Stmt, 500, "INSERT INTO SIMBAT_STATUS VALUES(?, ?, ?, ?, CURRENT TIMESTAMP)");

   r = SQLPrepare(InsertStmtHandle, (SQLCHAR*)Stmt, SQL_NTS);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLPrepare(insert) fail" , InsertStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertStmtHandle, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 40, 0,
      SIMBAT_STATUS.COL_SYSTEM, sizeof(SIMBAT_STATUS.COL_SYSTEM), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert, 1) fail" , InsertStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertStmtHandle, 2, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATUS.COL_IDX, sizeof(SIMBAT_STATUS.COL_IDX), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert, 2) fail" , InsertStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertStmtHandle, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0,
      SIMBAT_STATUS.COL_REC_TYPE, sizeof(SIMBAT_STATUS.COL_REC_TYPE), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert, 3) fail" , InsertStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertStmtHandle, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 2000, 0,
      SIMBAT_STATUS.COL_VAL, sizeof(SIMBAT_STATUS.COL_VAL), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert, 4) fail" , InsertStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLAllocHandle(SQL_HANDLE_STMT, ConnectHandle, &InsertHisStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLAllocHandle(STMT) fail" , ConnectHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   snprintf(Stmt, 500, "INSERT INTO SIMBAT_HIS VALUES(?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)");

   r = SQLPrepare(InsertHisStmtHandle, (SQLCHAR*)Stmt, SQL_NTS);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLPrepare(insert_his) fail" , InsertHisStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertHisStmtHandle, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 40, 0,
      SIMBAT_HIS.COL_SYSTEM, sizeof(SIMBAT_HIS.COL_SYSTEM), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_his, 1) fail" , InsertHisStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertHisStmtHandle, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 20, 0,
      SIMBAT_HIS.COL_BATCH_SEQ, sizeof(SIMBAT_HIS.COL_BATCH_SEQ), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_his, 2) fail" , InsertHisStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertHisStmtHandle, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 80, 0,
      SIMBAT_HIS.COL_JOB_NAME, sizeof(SIMBAT_HIS.COL_JOB_NAME), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_his, 3) fail" , InsertHisStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertHisStmtHandle, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 20, 0,
      SIMBAT_HIS.COL_START_TIME, sizeof(SIMBAT_HIS.COL_START_TIME), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_his, 4) fail" , InsertHisStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertHisStmtHandle, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 20, 0,
      SIMBAT_HIS.COL_END_TIME, sizeof(SIMBAT_HIS.COL_END_TIME), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_his, 5) fail" , InsertHisStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertHisStmtHandle, 6, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_HIS.COL_ELAPSE, sizeof(SIMBAT_HIS.COL_ELAPSE), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_his, 6) fail" , InsertHisStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertHisStmtHandle, 7, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0,
      SIMBAT_HIS.COL_STATUS, sizeof(SIMBAT_HIS.COL_STATUS), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_his, 7) fail" , InsertHisStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLAllocHandle(SQL_HANDLE_STMT, ConnectHandle, &InsertStaStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLAllocHandle(STMT) fail" , ConnectHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   snprintf(Stmt, 500, "INSERT INTO SIMBAT_STATISTIC VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)");

   r = SQLPrepare(InsertStaStmtHandle, (SQLCHAR*)Stmt, SQL_NTS);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLPrepare(insert_sta) fail" , InsertStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertStaStmtHandle, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 40, 0,
      SIMBAT_STATISTIC.COL_SYSTEM, sizeof(SIMBAT_STATISTIC.COL_SYSTEM), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_sta, 1) fail" , InsertStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertStaStmtHandle, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0,
      SIMBAT_STATISTIC.COL_BATCH_TYPE, sizeof(SIMBAT_STATISTIC.COL_BATCH_TYPE), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_sta, 2) fail" , InsertStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertStaStmtHandle, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 80, 0,
      SIMBAT_STATISTIC.COL_JOB_NAME, sizeof(SIMBAT_STATISTIC.COL_JOB_NAME), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_sta, 3) fail" , InsertStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertStaStmtHandle, 4, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_N, sizeof(SIMBAT_STATISTIC.COL_N), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_sta, 4) fail" , InsertStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertStaStmtHandle, 5, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE0, sizeof(SIMBAT_STATISTIC.COL_ELAPSE0), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_sta, 5) fail" , InsertStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertStaStmtHandle, 6, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE1, sizeof(SIMBAT_STATISTIC.COL_ELAPSE1), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_sta, 6) fail" , InsertStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertStaStmtHandle, 7, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE2, sizeof(SIMBAT_STATISTIC.COL_ELAPSE2), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_sta, 7) fail" , InsertStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertStaStmtHandle, 8, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE3, sizeof(SIMBAT_STATISTIC.COL_ELAPSE3), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_sta, 8) fail" , InsertStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertStaStmtHandle, 9, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE4, sizeof(SIMBAT_STATISTIC.COL_ELAPSE4), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_sta, 9) fail" , InsertStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertStaStmtHandle, 10, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE5, sizeof(SIMBAT_STATISTIC.COL_ELAPSE5), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_sta, 10) fail" , InsertStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertStaStmtHandle, 11, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE6, sizeof(SIMBAT_STATISTIC.COL_ELAPSE6), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_sta, 11) fail" , InsertStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertStaStmtHandle, 12, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE7, sizeof(SIMBAT_STATISTIC.COL_ELAPSE7), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_sta, 12) fail" , InsertStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertStaStmtHandle, 13, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE8, sizeof(SIMBAT_STATISTIC.COL_ELAPSE8), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_sta, 13) fail" , InsertStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(InsertStaStmtHandle, 14, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE9, sizeof(SIMBAT_STATISTIC.COL_ELAPSE9), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(insert_sta, 14) fail" , InsertStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLAllocHandle(SQL_HANDLE_STMT, ConnectHandle, &SelectStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLAllocHandle(STMT) fail" , ConnectHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   snprintf(Stmt, 500, "SELECT COL_REC_TYPE, COL_VAL FROM SIMBAT_STATUS WHERE COL_SYSTEM = ? AND COL_IDX = ?");

   r = SQLPrepare(SelectStmtHandle, (SQLCHAR*)Stmt, SQL_NTS);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLPrepare(select) fail" , SelectStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(SelectStmtHandle, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 40, 0,
      SIMBAT_STATUS.COL_SYSTEM, sizeof(SIMBAT_STATUS.COL_SYSTEM), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(select, 1) fail" , SelectStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(SelectStmtHandle, 2, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATUS.COL_IDX, sizeof(SIMBAT_STATUS.COL_IDX), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(select, 2) fail" , SelectStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLAllocHandle(SQL_HANDLE_STMT, ConnectHandle, &SelectStaStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLAllocHandle(STMT) fail" , ConnectHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   snprintf(Stmt, 500, "SELECT * FROM SIMBAT_STATISTIC WHERE COL_SYSTEM = ? AND COL_BATCH_TYPE = ? AND COL_JOB_NAME = ?");

   r = SQLPrepare(SelectStaStmtHandle, (SQLCHAR*)Stmt, SQL_NTS);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLPrepare(select_sta) fail" , SelectStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(SelectStaStmtHandle, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 40, 0,
      SIMBAT_STATISTIC.COL_SYSTEM, sizeof(SIMBAT_STATISTIC.COL_SYSTEM), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(select_sta, 1) fail" , SelectStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(SelectStaStmtHandle, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0,
      SIMBAT_STATISTIC.COL_BATCH_TYPE, sizeof(SIMBAT_STATISTIC.COL_BATCH_TYPE), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(select_sta, 2) fail" , SelectStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(SelectStaStmtHandle, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 80, 0,
      SIMBAT_STATISTIC.COL_JOB_NAME, sizeof(SIMBAT_STATISTIC.COL_JOB_NAME), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(select_sta, 3) fail" , SelectStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLAllocHandle(SQL_HANDLE_STMT, ConnectHandle, &UpdateStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLAllocHandle(STMT) fail" , ConnectHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   snprintf(Stmt, 500, "UPDATE SIMBAT_STATUS SET COL_REC_TYPE = ?, COL_VAL = ?, TS = CURRENT TIMESTAMP WHERE COL_SYSTEM = ? AND COL_IDX = ?");

   r = SQLPrepare(UpdateStmtHandle, (SQLCHAR*)Stmt, SQL_NTS);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLPrepare(update) fail" , UpdateStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateStmtHandle, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0,
      SIMBAT_STATUS.COL_REC_TYPE, sizeof(SIMBAT_STATUS.COL_REC_TYPE), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update, 1) fail" , UpdateStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateStmtHandle, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 2000, 0,
      SIMBAT_STATUS.COL_VAL, sizeof(SIMBAT_STATUS.COL_VAL), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update, 2) fail" , UpdateStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateStmtHandle, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 40, 0,
      SIMBAT_STATUS.COL_SYSTEM, sizeof(SIMBAT_STATUS.COL_SYSTEM), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update, 3) fail" , UpdateStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateStmtHandle, 4, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATUS.COL_IDX, sizeof(SIMBAT_STATUS.COL_IDX), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update, 3) fail" , UpdateStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLAllocHandle(SQL_HANDLE_STMT, ConnectHandle, &UpdateHisStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLAllocHandle(STMT) fail" , ConnectHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   snprintf(Stmt, 500, "UPDATE SIMBAT_HIS SET COL_START_TIME = ?, COL_END_TIME = ?, "
      "COL_ELAPSE = ?, COL_STATUS = ?, TS = CURRENT TIMESTAMP WHERE COL_SYSTEM = ? AND COL_BATCH_SEQ = ? AND COL_JOB_NAME = ?");

   r = SQLPrepare(UpdateHisStmtHandle, (SQLCHAR*)Stmt, SQL_NTS);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLPrepare(update_his) fail" , UpdateHisStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateHisStmtHandle, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 20, 0,
      SIMBAT_HIS.COL_START_TIME, sizeof(SIMBAT_HIS.COL_START_TIME), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_his, 1) fail" , UpdateHisStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateHisStmtHandle, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 20, 0,
      SIMBAT_HIS.COL_END_TIME, sizeof(SIMBAT_HIS.COL_END_TIME), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_his, 2) fail" , UpdateHisStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateHisStmtHandle, 3, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_HIS.COL_ELAPSE, sizeof(SIMBAT_HIS.COL_ELAPSE), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_his, 3) fail" , UpdateHisStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateHisStmtHandle, 4, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 10, 0,
      SIMBAT_HIS.COL_STATUS, sizeof(SIMBAT_HIS.COL_STATUS), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_his, 4) fail" , UpdateHisStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateHisStmtHandle, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 40, 0,
      SIMBAT_HIS.COL_SYSTEM, sizeof(SIMBAT_HIS.COL_SYSTEM), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_his, 5) fail" , UpdateHisStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateHisStmtHandle, 6, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 20, 0,
      SIMBAT_HIS.COL_BATCH_SEQ, sizeof(SIMBAT_HIS.COL_BATCH_SEQ), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_his, 6) fail" , UpdateHisStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateHisStmtHandle, 7, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 80, 0,
      SIMBAT_HIS.COL_JOB_NAME, sizeof(SIMBAT_HIS.COL_JOB_NAME), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_his, 7) fail" , UpdateHisStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLAllocHandle(SQL_HANDLE_STMT, ConnectHandle, &UpdateStaStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLAllocHandle(STMT) fail" , ConnectHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   snprintf(Stmt, 500, "UPDATE SIMBAT_STATISTIC SET COL_N = ?, COL_ELAPSE0 = ?, COL_ELAPSE1 = ?, COL_ELAPSE2 = ?, "
      "COL_ELAPSE3 = ?, COL_ELAPSE4 = ?, COL_ELAPSE5 = ?, COL_ELAPSE6 = ?, COL_ELAPSE7 = ?, COL_ELAPSE8 = ?, COL_ELAPSE9 = ?, "
      "TS = CURRENT TIMESTAMP WHERE COL_SYSTEM = ? AND COL_BATCH_TYPE = ? AND COL_JOB_NAME = ?");

   r = SQLPrepare(UpdateStaStmtHandle, (SQLCHAR*)Stmt, SQL_NTS);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLPrepare(update_sta) fail" , UpdateStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateStaStmtHandle, 1, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_N, sizeof(SIMBAT_STATISTIC.COL_N), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_sta, 1) fail" , UpdateStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateStaStmtHandle, 2, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE0, sizeof(SIMBAT_STATISTIC.COL_ELAPSE0), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_sta, 2) fail" , UpdateStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateStaStmtHandle, 3, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE1, sizeof(SIMBAT_STATISTIC.COL_ELAPSE1), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_sta, 3) fail" , UpdateStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateStaStmtHandle, 4, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE2, sizeof(SIMBAT_STATISTIC.COL_ELAPSE2), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_sta, 4) fail" , UpdateStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateStaStmtHandle, 5, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE3, sizeof(SIMBAT_STATISTIC.COL_ELAPSE3), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_sta, 5) fail" , UpdateStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateStaStmtHandle, 6, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE4, sizeof(SIMBAT_STATISTIC.COL_ELAPSE4), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_sta, 6) fail" , UpdateStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateStaStmtHandle, 7, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE5, sizeof(SIMBAT_STATISTIC.COL_ELAPSE5), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_sta, 7) fail" , UpdateStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateStaStmtHandle, 8, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE6, sizeof(SIMBAT_STATISTIC.COL_ELAPSE6), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_sta, 8) fail" , UpdateStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateStaStmtHandle, 9, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE7, sizeof(SIMBAT_STATISTIC.COL_ELAPSE7), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_sta, 9) fail" , UpdateStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateStaStmtHandle, 10, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE8, sizeof(SIMBAT_STATISTIC.COL_ELAPSE8), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_sta, 10) fail" , UpdateStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateStaStmtHandle, 11, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 10, 0,
      &SIMBAT_STATISTIC.COL_ELAPSE9, sizeof(SIMBAT_STATISTIC.COL_ELAPSE9), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_sta, 11) fail" , UpdateStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateStaStmtHandle, 12, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 40, 0,
      SIMBAT_STATISTIC.COL_SYSTEM, sizeof(SIMBAT_STATISTIC.COL_SYSTEM), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_sta, 12) fail" , UpdateStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateStaStmtHandle, 13, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 40, 0,
      SIMBAT_STATISTIC.COL_BATCH_TYPE, sizeof(SIMBAT_STATISTIC.COL_BATCH_TYPE), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_sta, 13) fail" , UpdateStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   r = SQLBindParameter(UpdateStaStmtHandle, 14, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 40, 0,
      SIMBAT_STATISTIC.COL_JOB_NAME, sizeof(SIMBAT_STATISTIC.COL_JOB_NAME), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError( "SQLBindCol(update_sta, 14) fail" , UpdateStaStmtHandle, SQL_HANDLE_DBC, ErrStr);
      return 8;
   }

   return 0;
}

static int CountStatus(const char *System, int *Result, char ErrStr[200 + 1])
{
   int r;
   SQLINTEGER n;

   *Result = 0;
   ErrStr[0] = '\0';
   strcpy(SIMBAT_STATUS.COL_SYSTEM, System);
   r = SQLExecute(CountStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLExecute(count) fail", CountStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLFetch(CountStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLFetch fail", CountStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLGetData(CountStmtHandle, 1, SQL_C_SLONG, &n, 0, NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLGetData fail", CountStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   *Result = n;
   r = SQLCloseCursor(CountStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLCloseCursor fail", CountStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   return 0;
}

static int Max(const char *System, int *Result, char ErrStr[200 + 1])
{
   int r;
   SQLINTEGER n;

   *Result = 0;
   ErrStr[0] = '\0';
   strcpy(SIMBAT_STATUS.COL_SYSTEM, System);
   r = SQLExecute(MaxStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLExecute(max) fail", MaxStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLFetch(MaxStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLFetch fail", MaxStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLGetData(MaxStmtHandle, 1, SQL_C_SLONG, &n, 0, NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLGetData fail", MaxStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   *Result = n;
   r = SQLCloseCursor(MaxStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLCloseCursor fail", MaxStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   return 0;
}

static int ClearStatus(const char *System, char ErrStr[200 + 1])
{
   int r;
   int TotalRec = 0;

   if ( Max(System, &TotalRec, ErrStr) != 0 )
   {
      return 8;
   }

   ErrStr[0] = '\0';
   strcpy(SIMBAT_STATUS.COL_SYSTEM, System);

   for ( int i = 1; i <= TotalRec / 10000 + 2; i++ )
   {
      SIMBAT_STATUS.COL_IDX = i * 10000;
      r = SQLExecute(DeleteStmtHandle);
      if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO && r != SQL_NO_DATA )
      {
         PrintError("SQLExecute(delete) fail", DeleteStmtHandle, SQL_HANDLE_STMT, ErrStr);
         return 8;
      }
      r = SQLExecute(CommitStmtHandle);
      if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
      {
         PrintError("SQLExecute(commit) fail", CommitStmtHandle, SQL_HANDLE_STMT, ErrStr);
         return 8;
      }
   }

   return 0;
}

static int WriteStatus(const char *System, int Idx, const char *RecType, const char *Record, char ErrStr[200 + 1])
{
   int r;

   ErrStr[0] = '\0';
   strcpy(SIMBAT_STATUS.COL_SYSTEM, System);
   SIMBAT_STATUS.COL_IDX = Idx;
   strcpy(SIMBAT_STATUS.COL_REC_TYPE, RecType);
   strcpy(SIMBAT_STATUS.COL_VAL, Record);
   r = SQLExecute(InsertStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLExecute(insert) fail", InsertStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLExecute(CommitStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLExecute(commit) fail", CommitStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   return 0;
}

static int ReadStatus(const char *System, int Idx, char *RecType, char *Record, char ErrStr[200 + 1])
{
   int r;

   ErrStr[0] = '\0';
   strcpy(SIMBAT_STATUS.COL_SYSTEM, System);
   SIMBAT_STATUS.COL_IDX = Idx;
   r = SQLExecute(SelectStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLExecute(select) fail", SelectStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLFetch(SelectStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLFetch fail", SelectStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLGetData(SelectStmtHandle, 1, SQL_C_CHAR, SIMBAT_STATUS.COL_REC_TYPE, sizeof(SIMBAT_STATUS.COL_REC_TYPE), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLGetData fail", SelectStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLGetData(SelectStmtHandle, 2, SQL_C_CHAR, SIMBAT_STATUS.COL_VAL, sizeof(SIMBAT_STATUS.COL_VAL), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLGetData fail", SelectStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLCloseCursor(SelectStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLCloseCursor fail", SelectStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   strcpy(RecType, SIMBAT_STATUS.COL_REC_TYPE);
   strcpy(Record, SIMBAT_STATUS.COL_VAL);
   return 0;
}

static int RewriteStatus(const char *System, int Idx, const char *RecType, const char *Record, char ErrStr[200 + 1])
{
   int r;

   ErrStr[0] = '\0';
   strcpy(SIMBAT_STATUS.COL_SYSTEM, System);
   SIMBAT_STATUS.COL_IDX = Idx;
   strcpy(SIMBAT_STATUS.COL_REC_TYPE, RecType);
   strcpy(SIMBAT_STATUS.COL_VAL, Record);
   r = SQLExecute(UpdateStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLExecute(update) fail", UpdateStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLExecute(CommitStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLExecute(commit) fail", CommitStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   return 0;
}

/*
   ValueList format :
   StartTime | EndTime | Elapse | JobStatus
 */
static int WriteHis(const char *System, const char *BatchSeq, const char *JobName, const char *ValueList, char ErrStr[200 + 1])
{
   int r;

   ErrStr[0] = '\0';
   strcpy(SIMBAT_HIS.COL_SYSTEM, System);
   strcpy(SIMBAT_HIS.COL_BATCH_SEQ, BatchSeq);
   strcpy(SIMBAT_HIS.COL_JOB_NAME, JobName);

   vector<string> StrVec;

   SplitString(ValueList, '|', StrVec, true);

   if ( StrVec.size() < 4 )
   {
      snprintf(ErrStr, 200, "value_list syntax error");
      return 8;
   }

   strcpy(SIMBAT_HIS.COL_START_TIME, StrVec[0].c_str());
   strcpy(SIMBAT_HIS.COL_END_TIME, StrVec[1].c_str());
   SIMBAT_HIS.COL_ELAPSE = atoi(StrVec[2].c_str());
   strcpy(SIMBAT_HIS.COL_STATUS, StrVec[3].c_str());
   r = SQLExecute(InsertHisStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      if ( r == SQL_DUPKEY1 )
      {
         r = SQLExecute(UpdateHisStmtHandle);
         if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
         {
            PrintError("SQLExecute(update_his) fail", UpdateHisStmtHandle, SQL_HANDLE_STMT, ErrStr);
            return 8;
         }
      }
      else
      {
         PrintError("SQLExecute(insert_his) fail", InsertHisStmtHandle, SQL_HANDLE_STMT, ErrStr);
         return 8;
      }
   }

   r = SQLExecute(CommitStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLExecute(commit) fail", CommitStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   return 0;
}

static int ReadStatistic(const char *System, const char *BatchType, const char *JobName, int *n, int Elapse[10], char ErrStr[200 + 1])
{
   int r;

   ErrStr[0] = '\0';
   strcpy(SIMBAT_STATISTIC.COL_SYSTEM, System);
   strcpy(SIMBAT_STATISTIC.COL_BATCH_TYPE, BatchType);
   strcpy(SIMBAT_STATISTIC.COL_JOB_NAME, JobName);

   *n = 0;

   for ( int i = 0; i < 10; i++ )
   {
      Elapse[i] = 0;
   }

   r = SQLExecute(SelectStaStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLExecute(select_sta) fail", SelectStaStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLFetch(SelectStaStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLFetch fail", SelectStaStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLGetData(SelectStaStmtHandle, 1, SQL_C_CHAR, SIMBAT_STATISTIC.COL_SYSTEM, sizeof(SIMBAT_STATISTIC.COL_SYSTEM), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLGetData_sta 1 fail", SelectStaStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLGetData(SelectStaStmtHandle, 2, SQL_C_CHAR, SIMBAT_STATISTIC.COL_BATCH_TYPE, sizeof(SIMBAT_STATISTIC.COL_BATCH_TYPE), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLGetData_sta 2 fail", SelectStaStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLGetData(SelectStaStmtHandle, 3, SQL_C_CHAR, SIMBAT_STATISTIC.COL_JOB_NAME, sizeof(SIMBAT_STATISTIC.COL_JOB_NAME), NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLGetData_sta 3 fail", SelectStaStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLGetData(SelectStaStmtHandle, 4, SQL_C_SLONG, &SIMBAT_STATISTIC.COL_N, 0, NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLGetData_sta 4 fail", SelectStaStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLGetData(SelectStaStmtHandle, 5, SQL_C_SLONG, &SIMBAT_STATISTIC.COL_ELAPSE0, 0, NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLGetData_sta 5 fail", SelectStaStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLGetData(SelectStaStmtHandle, 6, SQL_C_SLONG, &SIMBAT_STATISTIC.COL_ELAPSE1, 0, NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLGetData_sta 6 fail", SelectStaStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLGetData(SelectStaStmtHandle, 7, SQL_C_SLONG, &SIMBAT_STATISTIC.COL_ELAPSE2, 0, NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLGetData_sta 7 fail", SelectStaStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLGetData(SelectStaStmtHandle, 8, SQL_C_SLONG, &SIMBAT_STATISTIC.COL_ELAPSE3, 0, NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLGetData_sta 8 fail", SelectStaStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLGetData(SelectStaStmtHandle, 9, SQL_C_SLONG, &SIMBAT_STATISTIC.COL_ELAPSE4, 0, NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLGetData_sta 9 fail", SelectStaStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLGetData(SelectStaStmtHandle, 10, SQL_C_SLONG, &SIMBAT_STATISTIC.COL_ELAPSE5, 0, NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLGetData_sta 10 fail", SelectStaStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLGetData(SelectStaStmtHandle, 11, SQL_C_SLONG, &SIMBAT_STATISTIC.COL_ELAPSE6, 0, NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLGetData_sta 11 fail", SelectStaStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLGetData(SelectStaStmtHandle, 12, SQL_C_SLONG, &SIMBAT_STATISTIC.COL_ELAPSE7, 0, NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLGetData_sta 12 fail", SelectStaStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLGetData(SelectStaStmtHandle, 13, SQL_C_SLONG, &SIMBAT_STATISTIC.COL_ELAPSE8, 0, NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLGetData_sta 13 fail", SelectStaStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   r = SQLGetData(SelectStaStmtHandle, 14, SQL_C_SLONG, &SIMBAT_STATISTIC.COL_ELAPSE9, 0, NULL);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLGetData_sta 14 fail", SelectStaStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   *n = SIMBAT_STATISTIC.COL_N;
   Elapse[0] = SIMBAT_STATISTIC.COL_ELAPSE0;
   Elapse[1] = SIMBAT_STATISTIC.COL_ELAPSE1;
   Elapse[2] = SIMBAT_STATISTIC.COL_ELAPSE2;
   Elapse[3] = SIMBAT_STATISTIC.COL_ELAPSE3;
   Elapse[4] = SIMBAT_STATISTIC.COL_ELAPSE4;
   Elapse[5] = SIMBAT_STATISTIC.COL_ELAPSE5;
   Elapse[6] = SIMBAT_STATISTIC.COL_ELAPSE6;
   Elapse[7] = SIMBAT_STATISTIC.COL_ELAPSE7;
   Elapse[8] = SIMBAT_STATISTIC.COL_ELAPSE8;
   Elapse[9] = SIMBAT_STATISTIC.COL_ELAPSE9;
   return 0;
}

static int WriteStatistic(const char *System, const char *BatchType, const char *JobName, int n, int Elapse[10], char ErrStr[200 + 1])
{
   int r;

   ErrStr[0] = '\0';
   strcpy(SIMBAT_STATISTIC.COL_SYSTEM, System);
   strcpy(SIMBAT_STATISTIC.COL_BATCH_TYPE, BatchType);
   strcpy(SIMBAT_STATISTIC.COL_JOB_NAME, JobName);
   SIMBAT_STATISTIC.COL_N = n;
   SIMBAT_STATISTIC.COL_ELAPSE0 = Elapse[0];
   SIMBAT_STATISTIC.COL_ELAPSE1 = Elapse[1];
   SIMBAT_STATISTIC.COL_ELAPSE2 = Elapse[2];
   SIMBAT_STATISTIC.COL_ELAPSE3 = Elapse[3];
   SIMBAT_STATISTIC.COL_ELAPSE4 = Elapse[4];
   SIMBAT_STATISTIC.COL_ELAPSE5 = Elapse[5];
   SIMBAT_STATISTIC.COL_ELAPSE6 = Elapse[6];
   SIMBAT_STATISTIC.COL_ELAPSE7 = Elapse[7];
   SIMBAT_STATISTIC.COL_ELAPSE8 = Elapse[8];
   SIMBAT_STATISTIC.COL_ELAPSE9 = Elapse[9];
   r = SQLExecute(InsertStaStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      if ( r == SQL_DUPKEY1 )
      {
         r = SQLExecute(UpdateStaStmtHandle);
         if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
         {
            PrintError("SQLExecute(update_sta) fail", UpdateStaStmtHandle, SQL_HANDLE_STMT, ErrStr);
            return 8;
         }
      }
      else
      {
         PrintError("SQLExecute(insert_sta) fail", InsertStaStmtHandle, SQL_HANDLE_STMT, ErrStr);
         return 8;
      }
   }

   r = SQLExecute(CommitStmtHandle);

   if ( r != SQL_SUCCESS && r != SQL_SUCCESS_WITH_INFO )
   {
      PrintError("SQLExecute(commit) fail", CommitStmtHandle, SQL_HANDLE_STMT, ErrStr);
      return 8;
   }

   return 0;
}

#ifdef __cplusplus
extern "C" {
#endif

void SimbatPluginDbInit(SimbatPluginDbInitArg *Arg)
{
   Arg->Connect = DbConnect;
   Arg->CountStatus = CountStatus;
   Arg->ClearStatus = ClearStatus;
   Arg->WriteStatus = WriteRecord;
   Arg->ReadStatus = ReadStatus;
   Arg->RewriteStatus = RewriteStatus;
   Arg->WriteHis = WriteHis;
   Arg->ReadStatistic = ReadStatistic;
   Arg->WriteStatistic = WriteStatistic;
}

/*
int main()
{
   int n;
   char Sqlstate[5 + 1];
   char TmpStr1[200 + 1];

fprintf(stderr, "1\n");
   DbConnect(Sqlstate);
fprintf(stderr, "2\n");
   Count(&n, Sqlstate);
fprintf(stderr, "3, n = %d\n", n);
   Clear(Sqlstate);
fprintf(stderr, "4\n");
   WriteRecord(1, "abc", 3, Sqlstate);
fprintf(stderr, "5\n");
   memset(TmpStr1, 0, sizeof(TmpStr1));
   ReadRecord(1, TmpStr1, sizeof(TmpStr1), Sqlstate);
fprintf(stderr, "6, %s,\n", TmpStr1);
   strcpy(TmpStr1, "kkkk");
   RewriteRecord(1, TmpStr1, strlen(TmpStr1), Sqlstate);
fprintf(stderr, "7\n");
   memset(TmpStr1, 0, sizeof(TmpStr1));
   ReadRecord(1, TmpStr1, sizeof(TmpStr1), Sqlstate);
fprintf(stderr, "8, %s,\n", TmpStr1);
   return 0;
}
*/

#ifdef __cplusplus
}
#endif
